<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>	
    <link id="easyuiTheme" rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.3/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.3/themes/icon.css"/>
    <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.3/demo/demo.css"/>
    <script type="text/javascript" src="../jquery-easyui-1.5.3/jquery.min.js"></script>
    <script type="text/javascript" src="../jquery-easyui-1.5.3/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../jquery-easyui-1.5.3/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
	    var contextPath = function() {
	        return  "/" + location.pathname.split("/")[1];
	    }
	    
		function getValue(name){
			var str=window.location.search;
			if (str.indexOf(name)!=-1){
				var pos_start=str.indexOf(name)+name.length+1;
				var pos_end=str.indexOf("&",pos_start);
				if (pos_end==-1){
					return str.substring(pos_start);
				}	else	{
					return str.substring(pos_start,pos_end)
				}
			}	else	{
				return "";
			}
		}
		
		var  themeName = getValue("theme");
		if (themeName!=""){
		    var cssurl  = $('#easyuiTheme').attr('href');  
		    var href = cssurl.substring(0, cssurl.indexOf('themes')) + 'themes/' + themeName + '/easyui.css';  
		    $('#easyuiTheme').attr('href', href);  
		}

		
		// 统计用户 访问网站步骤记录
		var tjSecond = 0;
		var tjRandom = 0;
		window.setInterval(function () {
		    tjSecond ++;
		    alert(tjSecond);
		}, 10*1000);
		// 随机数
		tjRandom = (new Date()).valueOf();
		// 用户第一次访问页面记录部分数据
		window.onload = function () {
		    var tjArr = localStorage.getItem("jsArr") ? localStorage.getItem("jsArr") : '[]';
		    var dataArr = {
		        'tjRd' : tjRandom,
		        'url' : location.href,
		        'refer' : getReferrer()
		    };
		    tjArr = eval('(' + tjArr + ')');
		    tjArr.push(dataArr);
		    var tjArr1= JSON.stringify(tjArr);
		    localStorage.setItem("jsArr", tjArr1);
		};
		// 用户继续访问根据上面提供的key值补充数据
		window.onbeforeunload = function() {
		    var tjArrRd = eval('(' + localStorage.getItem("jsArr") + ')');
		    var tjI = tjArrRd.length - 1;
		    if(tjArrRd[tjI].tjRd == tjRandom){
		        tjArrRd[tjI].time = tjSecond;
		        tjArrRd[tjI].timeIn = Date.parse(new Date()) - (tjSecond * 1000);
		        tjArrRd[tjI].timeOut = Date.parse(new Date());
		        var tjArr1= JSON.stringify(tjArrRd);
		        localStorage.setItem("jsArr", tjArr1);
		    }
		};

		function getReferrer() {
		    var referrer = '';
		    try {
		        referrer = window.top.document.referrer;
		    } catch(e) {
		        if(window.parent) {
		            try {
		                referrer = window.parent.document.referrer;
		            } catch(e2) {
		                referrer = '';
		            }
		        }
		    }
		    if(referrer === '') {
		        referrer = document.referrer;
		    }
		    return referrer;
		}
	
	</script>

</head>

<style>
    .datagrid-btable{
        width:100%;
    }
    .datagrid-header-inner{
        width:100%;
    }
    .datagrid-header{
        width:100%;
    }
    .datagrid-htable{
        width:100%;
    }
    .datagrid-cell{margin:auto;}
    .fitem{text-align:center}
    .ftitle{text-align:center}
</style>

<body>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north',title:'学科分类',split:false" style="width:100%;height:40%;padding:1px;overflow:hidden;">   
    	 <table id="dg_course"  class="easyui-datagrid" style="width:100%;height:100%"></table>
         <div id="toolbar_course">		
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newCourse()">添加学科</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="removeCourse()">删除学科</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="editCourse()">修改信息</a>
        </div>
    </div>
    <div data-options="region:'center',title:'课程列表'">
        <table id="dg_lesson"  class="easyui-datagrid" style="width:100%;height:100%"></table>             
        <div id="toolbar_lesson">		
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newLesson()">添加课程</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="removeLesson()">删除课程</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="editLesson()">修改信息</a>
        </div>
    </div>
</div>

<div id="dialog_addLesson" class="easyui-dialog" 
	modal="true" closed="true" 
	style="width:400px;height:420px;padding:10px 20px" 
	buttons="#buttons_addLesson">
    <form id="form_addLesson" method="post" >
        <div class="fitem">
        	<label>所属学科ID:</label>
            <input type="text" name="courseID" class="easyui-textbox" required="true" readonly="readonly" style="align:left;width:30px"/>   
        	<label>所属学科:</label>
            <input type="text" name="courseName" class="easyui-textbox" required="true" readonly="readonly" style="align:left;width:120px"/>    
        </div>
        <br>
        <div class="fitem">
            <label>课程名称:</label>
            <input type="text" name="lessonName" class="easyui-textbox" required="true"  style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>课程简介:</label>
            <input type="text" name="lessonDesc" class="easyui-textbox" style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>主讲老师:</label>
            <input type="text" name="teacher" class="easyui-textbox" style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>讲师介绍:</label>
            <input type="text" name="teacherDesc" class="easyui-textbox" style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>课程路径:</label>
            <input type="text" name="lessonPath" class="easyui-textbox" name="message" data-options="multiline:true" style="width:250px;height:40px"/>
        </div>
        <br>
        <div class="fitem">
            <label>权重(数值越低越靠前):</label>
            <input type="text" name="priority" class="easyui-textbox" style="width:100px"/>
        </div>
    </form>
    <div id="buttons_addLesson">
    	<a href="#" class="easyui-linkbutton"  onclick="addLesson()">添加</a>
    	<a href="#" class="easyui-linkbutton"  onclick="javascript:$('#dialog_addLesson').dialog('close')">取消</a>
	</div>
</div>

<div id="dialog_modifyLesson" class="easyui-dialog" 
	modal="true" closed="true" 
	style="width:400px;height:480px;padding:10px 20px" 
	buttons="#buttons_modifyLesson">
    <form id="form_modifyLesson" method="post" >
        <div class="fitem">
            <label>课程编号:</label>
            <input type="text" name="lessonID"  readonly="readonly" class="easyui-textbox" style="align:left;width:50px"/>
        	<label>所属学科ID:</label>
            <input type="text" name="courseID" class="easyui-textbox" required="true" style="width:50px"/>
        </div>
        <br>
        <div class="fitem">
            <label>课程名称:</label>
            <input type="text" name="lessonName" class="easyui-textbox" required="true"  style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>课程简介:</label>
            <input type="text" name="lessonDesc" class="easyui-textbox" required="true" style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>主讲老师:</label>
            <input type="text" name="teacher" class="easyui-textbox" style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>讲师介绍:</label>
            <input type="text" name="teacherDesc" class="easyui-textbox" style="width:250px"/>
        </div>
        <br>
        <div class="fitem">
            <label>课程路径:</label>
            <input type="text" name="lessonPath" class="easyui-textbox" name="message" data-options="multiline:true" style="width:250px;height:40px"/>
        </div>
        <br>
        <div class="fitem">
            <label>权重(数值越低越靠前):</label>
            <input type="text" name="priority" class="easyui-textbox" style="width:100px"/>
        </div>
    </form>
    <div id="buttons_modifyLesson">
		<a href="#" class="easyui-linkbutton" onclick="saveLesson()">保存</a>
		<a href="#" class="easyui-linkbutton" onclick="javascript:$('#dialog_modifyLesson').dialog('close')">取消</a>
	</div>
</div>




</body>
   
<script type="text/javascript">

    $(function () {
    	
    	var url_getCourseListByCollegeID = contextPath()+'/airuc/getCourseListByCollegeID?collegeid=11';
    	$('#dg_course').datagrid({
    		loadMsg:'正在加载课程类别...',
    	    url:url_getCourseListByCollegeID,
    	    toolbar:'#toolbar_course',
    	    pagination:'true',
            pageSize:'10',
            pageList:[10,30],
            rownumbers:'true',
            fitColumns:'false', //不自动适应列宽
            singleSelect:'true', 
    	    columns:[[
    			{field:'courseID',title:'ID',width:60},
    			{field:'courseName',title:'学科大类',width:100},
    			{field:'courseDesc',title:'学科简介',width:200,align:'left'},
    			{field:'createTime',title:'创建时间',width:160},
    			{field:'lastEditTime',title:'更新时间',width:160}
    	    ]],
    	    onSelect:function(rowIndex, rowData){
    	    	$("#dg_lesson").datagrid({url: contextPath()+'/airuc/getLessonListByCourseID'});  
    	    	$('#dg_lesson').datagrid('load',{courseid:rowData.courseID});
    		},
    	    onClickRow:function(rowIndex, rowData){
    	    	$("#dg_lesson").datagrid({url: contextPath()+'/airuc/getLessonListByCourseID'});  
    	    	$('#dg_lesson').datagrid('load',{courseid:rowData.courseID});
    		}
    	});    	
    	
    	$('#dg_lesson').datagrid({
    		loadMsg:'正在加载课程列表...',
    	    url:'',
    	    toolbar:'#toolbar_lesson',
    	    pagination:'true',
            pageSize:'20',
            pageList:[20,50],
            rownumbers:'true',
            fitColumns:'false',
            singleSelect:'true', 
    	    columns:[[
    			{field:'lessonID',title:'ID',width:40,align:'center'},
    			{field:'lessonName',title:'课程名称',width:100,align:'center'},
    			{field:'lessonDesc',title:'课程简介',width:200,align:'left'},
    			{field:'teacher',title:'主讲老师',width:100,align:'center'},
    			{field:'teacherDesc',title:'讲师介绍',width:200,align:'left'},
    			{field:'createTime',title:'创建时间',width:160},
    			{field:'lastEditTime',title:'更新时间',width:160}
    	    ]]
    	});  
    })
    
   	//课程信息修改
	function newLesson(){
        //清除表单原来的信息
        $('#form_addLesson').form('clear');
	    //获取选中的是哪个学科
	    var row = $('#dg_course').datagrid('getSelected');
	    if (row){
	        $('#dialog_addLesson').dialog('open').dialog('setTitle','新建课程');
	        //显示未修改前的用户信息
	        $('#form_addLesson').form('load',row);
	    }
	}
    
    //新增课程按钮事件
    function addLesson(){
    	var row = $('#dg_course').datagrid('getSelected');
	    var urlstr;
	    if(row==null){
	           $.messager.show({
	               title: 'Error',
	               msg: 'Course ID is null'
	           });
	           $('#dialog_addLesson').dialog('close');	
	           $('#dg_lesson').datagrid('reload');
	    } else{
	    	urlstr = contextPath()+'/airuc/addLesson';
	    }
	    $('#form_addLesson').form('submit', {
	        url: urlstr,
	        onSubmit: function(){
	            return $(this).form('validate');
	        },
	        success: function(data){
	            var result = eval('('+data+')');
	            if (result.success){
	                $('#dialog_addLesson').dialog('close');	// close the dialog
	                $('#dg_lesson').datagrid('reload');			// reload the user data
	                $.messager.show({
	                    title: 'Success',
	                    msg: '添加成功'
	                });
	            } else {
	                $.messager.show({
	                    title: 'Error',
	                    msg: result.msg
	                });
	            }
	        }
	    });
    }
    
	//课程信息修改
	function editLesson(){
	    //选中某一行
	    var row = $('#dg_lesson').datagrid('getSelected');
	    if (row){
	        $('#dialog_modifyLesson').dialog('open').dialog('setTitle','修改信息');
	        //显示未修改前的用户信息
	        $('#form_modifyLesson').form('load',row);
	    }
	}
    
	//信息保存按钮事件
	function saveLesson(){
	    var row = $('#dg_lesson').datagrid('getSelected');
	    var urlstr;
	    if(row==null){
	           $.messager.show({
	               title: 'Error',
	               msg: 'lesson ID is null'
	           });
	           $('#dialog_modifyLesson').dialog('close');	
	           $('#dg_lesson').datagrid('reload');
	    } else{
	    	urlstr = contextPath()+'/airuc/modifyLesson';
	    }
	    $('#form_modifyLesson').form('submit', {
	        url: urlstr,
	        onSubmit: function(){
	            return $(this).form('validate');
	        },
	        success: function(data){
	            var result = eval('('+data+')');
	            if (result.success){
	                $('#dialog_modifyLesson').dialog('close');	// close the dialog
	                $('#dg_lesson').datagrid('reload');			// reload the user data
	                $.messager.show({
	                    title: 'Success',
	                    msg: '保存成功'
	                });
	            } else {
	                $.messager.show({
	                    title: 'Error',
	                    msg: result.msg
	                });
	            }
	        }
	    });
	}
	
	//删除按钮事件
	function removeLesson(){
		var removeLessonURL = contextPath()+'/airuc/removeLesson';
	    var row = $('#dg_lesson').datagrid('getSelected');
	    if (row){
	        $.messager.confirm('Confirm','确定要删除所选择的课程吗?',function(r){
	            if (r){
	                $.post(removeLessonURL, {lessonID:row.lessonID}, function(result){
	                    if (result.success){	
	                        $('#dg_lesson').datagrid('reload');	// reload the user data
	                        $.messager.show({
	                            title: 'Success',
	                            msg: '删除成功'
	                        });
	                    } else {
	                        $.messager.show({	// show error message
	                            title: 'Error',
	                            msg: result.msg
	                        });
	                    }
	                },'json');
	            }
	        });
	    }
	}	

    
    //将表单数据转为json
    function form2Json(id) {

        var arr = $("#" + id).serializeArray();
        var jsonStr = "";
        jsonStr += '{';
        for (var i = 0; i < arr.length; i++) {
            jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",'
        }
        jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
        jsonStr += '}';
        var json = JSON.parse(jsonStr);
        return json;
    }
</script>
</html>